multiselection: Rewrite the select_callback implementation
authorMatthias Clasen <mclasen@redhat.com>
Sat, 6 Jun 2020 15:21:29 +0000 (11:21 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 6 Jun 2020 15:22:48 +0000 (11:22 -0400)
Use a for loop to make this more obvious, and add some
assertions that the callback is behaving properly.

gtk/gtkmultiselection.c

index 11caa78fc15840bd1961ebe0bb3ddef9c55650d8..a852972beea9b806c6b9140cdccbcafb07b0fa43 100644 (file)
@@ -180,32 +180,40 @@ gtk_multi_selection_add_or_remove (GtkSelectionModel    *model,
                                    gpointer              data)
 {
   GtkMultiSelection *self = GTK_MULTI_SELECTION (model);
-  guint pos, start, n;
+  guint pos, start, n_items;
   gboolean in;
   guint min, max;
+  guint n;
+
+  n = g_list_model_get_n_items (G_LIST_MODEL (self));
 
   min = G_MAXUINT;
   max = 0;
 
-  pos = 0;
-  do
+  for (pos = 0; pos < n; pos = start + n_items)
     {
-      callback (pos, &start, &n, &in, data);
+      callback (pos, &start, &n_items, &in, data);
+
+      if (n_items == 0)
+        break;
+
+      g_assert (start <= pos && pos < start + n_items);
+
       if (in)
         {
           if (start < min)
             min = start;
-          if (start + n - 1 > max)
-            max = start + n - 1;
+          if (start + n_items - 1 > max)
+            max = start + n_items - 1;
 
           if (add)
-            gtk_set_add_range (self->selected, start, n);
+            gtk_set_add_range (self->selected, start, n_items);
           else
-            gtk_set_remove_range (self->selected, start, n);
+            gtk_set_remove_range (self->selected, start, n_items);
         }
-      pos = start + n;
+
+      pos = start + n_items;
     }
-  while (n > 0);
 
   if (min <= max)
     gtk_selection_model_selection_changed (model, min, max - min + 1);